package ui;
/**
 * EDCTableModel
 *  Table view used to show and edit URL catalogs describing DTDs in the specified folder
 */
import java.io.File;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import utils.EDCUtils;

public class EDCTableModel extends AbstractTableModel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	

	private boolean DEBUG = false;
	
	private String[] columnNames = {"DTD file Name","Link","Description","Comment"};
	private Vector<Vector<Object>> data;
	
	public EDCTableModel() {
		data = new Vector<Vector<Object>>();
	}
	
//	public void setData(String folder) {
//		File[] files = EDCUtils.getFiles(folder);
//		if(files.length>0) {
//			for(int i=0; i<files.length; i++) {
//				data.add(EDCUtils.getData(files[i]));
//			}
//		}
//		else {
//			System.out.println("directory empty!");
//		}
//	}
	
	public void setData(String file) {
		try {
			data = EDCUtils.getXMLData(file);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(data==null) {
			File[] files = EDCUtils.getFiles(file);
			if(files.length>0) {
				for(int i=0; i<files.length; i++) {
					data.add(EDCUtils.getData(files[i]));
				}
			}
			else {
				System.out.println("directory empty!");
			}
		}
	}
	
	public Vector<Vector<Object>> getData() {
		return data;
	}
	
	
	public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return data.size();
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
        return data.get(row).get(col);
    }



    /*
     * This table's editable.
     */
    public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
        if (col < 2) {
            return false;
        } else {
            return true;
        }
    }

    /*
     * This table's data can change.
     */
    public void setValueAt(Object value, int row, int col) {
        if (DEBUG) {
            System.out.println("Setting value at " + row + "," + col
                               + " to " + value
                               + " (an instance of "
                               + value.getClass() + ")");
        }

//        data[row][col] = value;
        data.get(row).set(col, value);
        fireTableCellUpdated(row, col);

        if (DEBUG) {
            System.out.println("New value of data:");
            printDebugData();
        }
    }
    
    private void printDebugData() {
        int numRows = getRowCount();
        int numCols = getColumnCount();

        for (int i=0; i < numRows; i++) {
            System.out.print("    row " + i + ":");
            for (int j=0; j < numCols; j++) {
                System.out.print("  " + data.get(i).get(j));
            }
            System.out.println();
        }
        System.out.println("--------------------------");
    }
    
    
	
}
